package com.example.demo.module.supplier.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.service.impl.BaseServiceImpl;
import com.example.demo.entity.pojo.supplier.SupplierPoJo;
import com.example.demo.entity.request.supplier.SupplierRequest;
import com.example.demo.module.supplier.mapper.SupplierMapper;
import com.example.demo.module.supplier.service.SupplierService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * .
 *
 * @author Mei Ruoxiao
 * @since 2020/9/16
 */
@Slf4j
@Service
public class SupplierServiceImpl extends BaseServiceImpl<SupplierMapper, SupplierPoJo> implements SupplierService {

    @Override
    public long create(SupplierRequest request, long companyId) {
        log.info("create:[request:{}, companyId:{}]", request, companyId);
        SupplierPoJo poJo = SupplierPoJo.of(request);
        poJo.setCompanyId(companyId);
        long id = create(poJo).getId();
        return id;
    }

    @Override
    public Page<SupplierPoJo> page(String name, String boss, String tel, int page, int size, long companyId) {
        log.info("page:[name:{}, boss:{}, tel:{}, page:{}, size:{}, companyId:{}]", name, boss, tel, page, size, companyId);
        return this.page(Wrappers.<SupplierPoJo>lambdaQuery()
                        .eq(SupplierPoJo::getCompanyId, companyId)
                        .like(Objects.nonNull(name), SupplierPoJo::getName, name)
                        .or()
                        .like(Objects.nonNull(boss), SupplierPoJo::getBoss, boss)
                        .or()
                        .like(Objects.nonNull(tel), SupplierPoJo::getTel, tel),
                page,
                size,
                OrderItem.desc("create_time"));
    }
}
